<?php
require('../includes/functions.inc.php');
require('../includes/LITTLE_MonitorExcelReader.class.php');

$smarty = SmartyManager::getSmarty();

/*************************************************************
 * 每个上传文件只要修改下面这些变量就基本可以了
 ************************************************************/
$formFilename = 'monitorFile';

//不可缺少的列
$obligatoCols = array(
);

//电子表格和数据库表中的列的对应关系
$tableCols = array(
		'产品质量证明书号'   => 'certificationNumber',
		'产品质量证明书编号'   => 'certificationCodification', 				
		'批次'              => 'sequenceNumber',
		'生产厂家'	        => 'manufactory',						
		'船级'	        => 'authenticationMark',					
		'材质'            =>  'steelNumber',						
		'厚'            =>  'size1',						
		'宽'			    =>  'size2',						
		'长'	            =>  'size3',						
		'数量'	            =>  'count',						
		'单重'	            =>  'unitWeight',						
		'订单号'	            =>  'orderNumber',						
		'订单子项号'	        =>  'orderSubitemNumber',						
		'受订单价'          =>	'unitPrice',						
		'物料号'            =>  'materialNumber',						
		'批号'	        =>  'sampleBatch',						
		'发车证书号'	        =>  'facheNumber',					
		'购单号'     	    =>  'purchaseNumber',						
		'发货时间'           =>  'shipmentDate',								
		'合同月份'			=>	'month',						
		'车号'	            =>  'vehicleNumber',						
		'销售产家'  	=>  'consignee'
);

//对比时用来比较的列
$compareCols = array();

//获取备注信息
$record = Filter::forDBInsertion($_POST['record'], true);

/************************************************************
 * 文件上传
 ***********************************************************/
try{
	//$file 在本程序中用作了全局变量，用来在需要filename的函数中使用
	$file = saveUploadedFile($formFilename);
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	exit();
}

try{
	beginTransaction();
	
	//然后read上传的表格的信息，将其中的数据存入数据库中
	$monitorReader = new LITTLE_MonitorExcelReader();
	$monitorReader->read($file->getNewPath());

	$monitorReader->obligatoColumns = $obligatoCols;
	$monitorReader->tableColumns = $tableCols;
	//$monitorReader->compareColumns = $compareCols;

	$warnMsg = "";
	
	//主要是检查是否有未知的列，有的话就抛出异常
	$monitorReader->checkMyFirstRow();
	
	//文件是否重复上传
	if(($date = $monitorReader->myHaveUploaded())!=false){
		throw new UploadSameFileException($file->getFilename(), $date);
	}else {
		$monitorReader->saveFileInfo($record);
	}
	
	//这个放在saveFileInfo下面是因为如果放在上边的话，保存file信息后获得不到inser_id。
	//beginTransaction();
	$sameCertificationNumbers =array();
	if($sameCertificationNumbers = $monitorReader->haveSameCertificationNumber()){
		$warnMsg .= "<br />表格上传完成，但是程序检查到上传的文件中有重复的产品质量证明书号：'".join("', '", $sameCertificationNumbers)."', 如果你确认这里出现了问题，可以直接在系统中进行修改或者删除刚才上传的文件";
	}

	$monitorReader->myReadIntoDB();
	LogInserter::logForInsert("导入监控文件：{$file->getFilename()}");
}catch(DBQueryException $e){
	$smarty->assign('errMsg', $e);
	$smarty->assign('errorType', 'monitorUpload');
	$smarty->assign("errTitle", '上传监控文件失败1');
	$smarty->display('error.html');
	
	rollback();
	exit();
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->assign('errorType', 'monitorUpload');
	$smarty->assign("errTitle", '上传监控文件失败2');
	$smarty->display('error.html');
	rollback();
	exit();
}

$smarty->assign('successMsg', $file->getFilename().'上传成功');
$smarty->assign("successTitle", '上传监控文件成功');
$smarty->assign('successType', 'monitorUpload');
if(isset($warnMsg)){
	$smarty->assign('warnMsg', $warnMsg);
}
$smarty->display('success.html');
commit();
?>